package com.paic.mhis.hcpms.assessment.web;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.assessment.biz.service.ExportWordService;
import com.paic.mhis.hcpms.assessment.biz.service.PerformanceDataService;
import com.paic.mhis.hcpms.assessment.common.AssessmentConstants;
import com.paic.mhis.hcpms.assessment.dto.PerformanceDataDTO;
import com.paic.mhis.hcpms.assessment.dto.PerformanceThreeDataDTO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 
 * Description:数据录入模块
 * 
 * @author: EX-HUANGJUN003
 * @version:
 */
@Controller
@RequestMapping("/performance/")
public class PerformanceDataController {
	private static final Log LOG = LogFactory
			.getLog(PerformanceDataController.class);

	@BizServicesProxy(beanName = "performanceDataService")
	private PerformanceDataService performanceDataService;

	@BizServicesProxy(beanName = "exportWordService")
	private ExportWordService exportWordService;

	/**
	 * 
	 * Description: 总得分信息查询
	 * 
	 * @param regionCode
	 * @param kpiTime
	 * @param providerCode
	 * @param grade
	 * @return
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-5
	 *         mhis_hcpms EX-HUANGJUN003 创建
	 * @throws BusinessServiceException
	 */
	@RequestMapping(value = "score.do")
	@ResponseBody
	public Map<String, Object> getPerformanceScoreList(
			@RequestParam(value = "kpiTime", defaultValue = "")
			String kpiTime,
			@RequestParam(value = "providerCode", defaultValue = "")
			String providerCode) {
		List<PerformanceDataDTO> levelList;
		List<PerformanceDataDTO> levelList3;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String coding = providerCode;
			Map<String, Object> parameterMap = exportWordService
					.getHospitalInfo(coding);
			Map<String, Object> dateMapParameter = new HashMap<String, Object>();
			dateMapParameter.put("grade", parameterMap.get("HOSLEVEL"));
			dateMapParameter.put("regioncode", parameterMap.get("REGIONCODE"));

			String structureNo = exportWordService
					.getStructureNo(dateMapParameter);

			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("providerCode", providerCode);
			paramMap.put("kpiTime", kpiTime);
			paramMap.put("grade", parameterMap.get("HOSLEVEL"));
			paramMap.put("regionCode", parameterMap.get("REGIONCODE"));
			paramMap.put("structureNo", structureNo);
			paramMap.put("indicatorLevel", AssessmentConstants.INDICATOR_LEVEL0);
			levelList = performanceDataService.getPerformanceDataList(paramMap);
			paramMap.put("indicatorLevel", AssessmentConstants.INDICATOR_LEVEL3);
			levelList3 = performanceDataService
					.getPerformanceDataList(paramMap);

			result.put(WarnConstants.STATE_KEY,
					WarnConstants.SUCCESS_STATE_VALUE);
			result.put(WarnConstants.MSG_KEY, WarnConstants.SELECT_SUCCESS);
			List<PerformanceDataDTO> levelEnterList3 = new ArrayList<PerformanceDataDTO>();
			for (PerformanceDataDTO map3 : levelList3) {
				if (StringUtils.equals(map3.getSourceType(), "2")) {
					levelEnterList3.add(map3);
				}
			}
			List<PerformanceDataDTO> enterList = changeEnterDto(levelEnterList3);

			result.put("list", levelList);
			result.put("list3", enterList);
		} catch (BusinessServiceException e) {
			LOG.error(e, e);
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 
	 * Description: 各指标信息查询
	 * 
	 * @param regionCode
	 * @param kpiTime
	 * @param providerCode
	 * @param grade
	 * @return
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-5
	 *         mhis_hcpms EX-HUANGJUN003 创建
	 */
	@RequestMapping(value = "list.do")
	@ResponseBody
	public Map<String, Object> getPerformanceDataList(
			@RequestParam(value = "kpiTime", defaultValue = "")
			String kpiTime,
			@RequestParam(value = "providerCode", defaultValue = "")
			String providerCode) {
		List<PerformanceDataDTO> resultList;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String coding = providerCode;
			Map<String, Object> parameterMap = exportWordService
					.getHospitalInfo(coding);
			Map<String, Object> dateMapParameter = new HashMap<String, Object>();
			dateMapParameter.put("grade", parameterMap.get("HOSLEVEL"));
			dateMapParameter.put("regioncode", parameterMap.get("REGIONCODE"));

			String structureNo = exportWordService
					.getStructureNo(dateMapParameter);
			// Map参数
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("providerCode", providerCode);
			paramMap.put("kpiTime", kpiTime);
			paramMap.put("grade", parameterMap.get("HOSLEVEL"));
			paramMap.put("regionCode", parameterMap.get("REGIONCODE"));
			paramMap.put("structureNo", structureNo);
			paramMap.put("indicatorLevel", "");
			resultList = performanceDataService
					.getPerformanceDataList(paramMap);

			result.put(WarnConstants.STATE_KEY,
					WarnConstants.SUCCESS_STATE_VALUE);
			result.put(WarnConstants.MSG_KEY, WarnConstants.SELECT_SUCCESS);
			result.put("list", resultList);
		} catch (BusinessServiceException e) {
			LOG.error(e, e);
			result = HttpProcessUtil.processException(e);
		}

		return result;
	}

	/**
	 * 
	 * Description: 三级指标录入分数信息保存
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * 
	 *             Modification History: Date Version Author Description
	 *             --------
	 *             ----------------------------------------------------------
	 *             2015-3-5 mhis_hcpms EX-HUANGJUN003 创建
	 */
	@RequestMapping(value = "insert.do")
	@ResponseBody
	public Map<String, Object> saveScore(HttpServletRequest request)
			throws IOException {
		// 获取参数
		String param = HttpProcessUtil.readRequestBody(request);
		List<PerformanceDataDTO> resultList = new ArrayList<PerformanceDataDTO>();
		List<PerformanceDataDTO> list = JSONArray.parseArray(param,
				PerformanceDataDTO.class);
		boolean flag = false;
		String providerCode = "";
		String kpiTime = "";
		int time = 1;
		for (PerformanceDataDTO item : list) {
			if (time == 1) {
				providerCode = item.getProviderCode();
				kpiTime = item.getKpiTime();
				if (StringUtils.isEmpty(providerCode)
						|| StringUtils.isEmpty(kpiTime)) {
					flag = false;
				} else {
					flag = true;
				}
			}
			time++;
		}
		Map<String, Object> providerInfo = exportWordService
				.getHospitalInfo(providerCode);
		// 对象转换
		List<PerformanceThreeDataDTO> resultThreeDto = changeThreeDto(list,
				(String) providerInfo.get("REGIONCODE"));
		String msg = "";
		if (flag == true) {
			try {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("providerCode", providerCode);
				map.put("period", AssessmentConstants.MONTH);
				map.put("kpiTime", kpiTime);
				map.put("regionCode", providerInfo.get("REGIONCODE"));

				performanceDataService.insertThreeList(resultThreeDto, map);

				msg = "保存成功";
			} catch (Exception e) {
				LOG.error(e, e);
				msg = "保存失败";
			}
		} else {
			msg = "保存失败，没有指定医院和时间";
		}
		Map<String, Object> resp = new HashMap<String, Object>();
		resp.put("msg", msg);

		return resp;
	}

	/**
	 * 
	 * Description: 对象转换
	 * 
	 * @param list
	 * @return
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-6
	 *         mhis_hcpms EX-HUANGJUN003 创建
	 */
	private List<PerformanceDataDTO> changeEnterDto(
			List<PerformanceDataDTO> list) {
		int count = 1;
		List<PerformanceDataDTO> result = new ArrayList<PerformanceDataDTO>();
		PerformanceDataDTO searchVo = null;

		for (PerformanceDataDTO map3 : list) {
			// if(StringUtils.equals(map3.getSourceType(),"2")){
			if (count % 2 == 1) {
				searchVo = new PerformanceDataDTO();
				searchVo.setName1(map3.getIndicatorName());
				searchVo.setScore1(map3.getStructureScore());
				searchVo.setSourceType1(map3.getSourceType());
				searchVo.setIndicatorCode1(map3.getIndicatorCode());
				searchVo.setPercentScore1(map3.getPercentScore());
				searchVo.setVisitType1(map3.getVisitType());
				searchVo.setFullMark1(map3.getFullMark());
				searchVo.setStandardType1(map3.getStandardType());
				searchVo.setIndicatorValue1(map3.getIndicatorValue());
				searchVo.setUpperLimit1(map3.getUpperLimit());
				searchVo.setLowerLimit1(map3.getLowerLimit());
				searchVo.setUnit1(map3.getUnit());
				searchVo.setValue1(map3.getValue());
				searchVo.setEvaluationMethod1(map3.getEvaluationMethod());
				searchVo.setKpiTime(map3.getKpiTime());
				searchVo.setProviderCode(map3.getProviderCode());
				searchVo.setRegionCode(map3.getRegionCode());
			} else if (count % 2 == 0) {
				searchVo.setName2(map3.getIndicatorName());
				searchVo.setScore2(map3.getStructureScore());
				searchVo.setSourceType2(map3.getSourceType());
				searchVo.setIndicatorCode2(map3.getIndicatorCode());
				searchVo.setPercentScore2(map3.getPercentScore());
				searchVo.setVisitType2(map3.getVisitType());
				searchVo.setFullMark2(map3.getFullMark());
				searchVo.setStandardType2(map3.getStandardType());
				searchVo.setIndicatorValue2(map3.getIndicatorValue());
				searchVo.setUpperLimit2(map3.getUpperLimit());
				searchVo.setLowerLimit2(map3.getLowerLimit());
				searchVo.setUnit2(map3.getUnit());
				searchVo.setEvaluationMethod2(map3.getEvaluationMethod());
				searchVo.setValue2(map3.getValue());
			}

			if (count % 2 == 0 || count == list.size()) {
				result.add(searchVo);
			}

			count++;
		}

		// }
		return result;
	}

	/**
	 * 
	 * Description: 对象转换
	 * 
	 * @param listDto
	 * @return
	 * 
	 *         Modification History: Date Version Author Description
	 *         ------------
	 *         ------------------------------------------------------ 2015-3-6
	 *         mhis_hcpms EX-HUANGJUN003 创建
	 */
	private List<PerformanceThreeDataDTO> changeThreeDto(
			List<PerformanceDataDTO> listDto, String regionCode) {
		List<PerformanceThreeDataDTO> threeList = new ArrayList<PerformanceThreeDataDTO>();
		PerformanceThreeDataDTO threeDto = null;

		for (PerformanceDataDTO item : listDto) {
			threeDto = new PerformanceThreeDataDTO();
			threeDto.setIndicatorCode(item.getIndicatorCode1());
			threeDto.setStructureScore(item.getScore1());
			threeDto.setIndicatorValue(item.getThirdScore1());
			threeDto.setPercentScore(item.getPercentScore1());
			threeDto.setSourceType(item.getSourceType1());
			threeDto.setStandardType(item.getStandardType1());
			threeDto.setLowerLimit(item.getLowerLimit1());
			threeDto.setUpperLimit(item.getUpperLimit1());
			threeDto.setRegionCode(regionCode);
			threeDto.setProviderCode(item.getProviderCode());
			threeDto.setKpiTime(item.getKpiTime());
			threeDto.setPeriod(AssessmentConstants.MONTH);
			threeList.add(threeDto);

			threeDto = new PerformanceThreeDataDTO();
			threeDto.setIndicatorCode(item.getIndicatorCode2());
			threeDto.setStructureScore(item.getScore2());
			threeDto.setIndicatorValue(item.getThirdScore2());
			threeDto.setPercentScore(item.getPercentScore2());
			threeDto.setSourceType(item.getSourceType2());
			threeDto.setStandardType(item.getStandardType2());
			threeDto.setLowerLimit(item.getLowerLimit2());
			threeDto.setUpperLimit(item.getUpperLimit2());
			threeDto.setRegionCode(regionCode);
			threeDto.setProviderCode(item.getProviderCode());
			threeDto.setKpiTime(item.getKpiTime());
			threeDto.setPeriod(AssessmentConstants.MONTH);
			threeList.add(threeDto);
		}

		return threeList;
	}
}
